﻿using AntdUI;
using Super.Admin.Entities;
using Super.Admin.Global;
using Super.Admin.Models.User;

namespace Super.Admin.Mappers
{
    /// <summary>
    /// UserMapper 类用于在 UserEntity 和 UserModel 之间进行转换。
    /// </summary>
    public static class UserMapper
    {
        /// <summary>
        /// 将 UserEntity 转换为 UserBaseModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static UserBaseModel ToBaseModel(this UserEntity entity)
        {
            if (entity == null) return null;
            return new UserBaseModel
            {
                UserId = entity.UserId,
                UserName = entity.UserName,
                Gender = entity.Gender,
                DepartmentId = entity.DepartmentId,
                Gravatar = string.IsNullOrEmpty(entity.Gravatar) ? "" : FilePath.GravatarFilePath + entity.Gravatar,
                Password = entity.Password,
                NickName = entity.NickName,
                Email = entity.Email,
                Phone = entity.Phone,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                IsActive = entity.IsActive,
                LastLoginTime = entity.LastLoginTime,
                PersonalizedSignature = entity.PersonalizedSignature
            };
        }
        /// <summary>
        /// 将 UserEntity 转换为 UserDetatilModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static UserDetatilModel ToDetailModel(this UserEntity entity)
        {
            if (entity == null) return null;
            return new UserDetatilModel
            {
                UserId = entity.UserId,
                UserName = entity.UserName,
                Gender = entity.Gender,
                DepartmentId = entity.DepartmentId,
                Gravatar = string.IsNullOrEmpty(entity.Gravatar) ? "" : FilePath.GravatarFilePath + entity.Gravatar,
                Password = entity.Password,
                NickName = entity.NickName,
                Email = entity.Email,
                Phone = entity.Phone,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                IsActive = entity.IsActive,
                LastLoginTime = entity.LastLoginTime,
                PersonalizedSignature = entity.PersonalizedSignature,
                Department = entity.Department?.ToBaseModel(),
                UserRoles = (List<Models.UserRole.UserRoleDetailModel>)(entity.UserRoles?.ToDetailModels()),
                ManangerDepartments = (List<Models.Department.DepartmentBaseModel>)(entity.ManangerDepartments?.ToBaseModels()),
                DepartmentName = entity.Department?.ToBaseModel().Name,
                IsActiveTag = entity.IsActive == 0 ? new CellTag[] { new CellTag("启用", TTypeMini.Success) } : entity.IsActive == 1 ? new CellTag[] { new CellTag("停用", TTypeMini.Error) } : new CellTag[] { new CellTag("未知", TTypeMini.Default) },
                GenderTag = entity.Gender == 0 ? new CellTag[] { new CellTag("女", TTypeMini.Error) } : entity.Gender == 1 ? new CellTag[] { new CellTag("男", TTypeMini.Primary) } : new CellTag[] { new CellTag("未知", TTypeMini.Default) },
            };
        }
        /// <summary>
        /// 将 UserBaseModel 转换为 UserEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static UserEntity ToEntity(this UserBaseModel model)
        {
            if (model == null) return null;
            return new UserEntity
            {
                UserId = model.UserId,
                UserName = model.UserName,
                Gender = model.Gender,
                DepartmentId = model.DepartmentId,
                Gravatar = model.Gravatar,
                Password = model.Password,
                NickName = model.NickName,
                Email = model.Email,
                Phone = model.Phone,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                IsActive = model.IsActive,
                LastLoginTime = model.LastLoginTime,
                PersonalizedSignature = model.PersonalizedSignature
            };
        }
        /// <summary>
        /// 将 UserDetatilModel 转换为 UserEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static UserEntity ToEntity(this UserDetatilModel model)
        {
            if (model == null) return null;
            return new UserEntity
            {
                UserId = model.UserId,
                UserName = model.UserName,
                Gender = model.Gender,
                DepartmentId = model.DepartmentId,
                Gravatar = model.Gravatar,
                Password = string.IsNullOrEmpty(model.Password) ? "123456" : model.Password,
                NickName = model.NickName,
                Email = model.Email,
                Phone = model.Phone,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                IsActive = model.IsActive,
                LastLoginTime = model.LastLoginTime,
                PersonalizedSignature = model.PersonalizedSignature
            };
        }
        /// <summary>
        /// 将 UserEntity 列表转换为 UserBaseModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<UserBaseModel> ToBaseModels(this IEnumerable<UserEntity> entities)
        {
            return entities.Select(e => e.ToBaseModel()).ToList();
        }
        /// <summary>
        /// 将 UserEntity 列表转换为 UserDetatilModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<UserDetatilModel> ToDetailModels(this IEnumerable<UserEntity> entities)
        {
            return entities.Select(e => e.ToDetailModel()).ToList();
        }
        /// <summary>
        /// 将 UserDetatilModel 列表转换为 UserEntity 列表。
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static IEnumerable<UserEntity> ToEntities(this IEnumerable<UserDetatilModel> models)
        {
            return models.Select(m => m.ToEntity()).ToList();
        }
        /// <summary>
        /// 将 UserBaseModel 列表转换为 UserEntity 列表。
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static IEnumerable<UserEntity> ToEntities(this IEnumerable<UserBaseModel> models)
        {
            return models.Select(m => m.ToEntity()).ToList();
        }
    }
}
